define(['jquery','mui','form','page', 'picker'], function($,$$,Form,Page,undefined) {
    var Controller = {
        index: function () {
            //扩展mui.showLoading
            (function($, window) {
                //显示加载框
                $.showLoading = function(message,type) {
                    if ($.os.plus && type !== 'div') {
                        $.plusReady(function() {
                            plus.nativeUI.showWaiting(message);
                        });
                    } else {
                        var html = '';
                        html += '<i class="mui-spinner mui-spinner-white"></i>';
                        html += '<p class="text">' + (message || "数据加载中") + '</p>';

                        //遮罩层
                        var mask=document.getElementsByClassName("mui-show-loading-mask");
                        if(mask.length==0){
                            mask = document.createElement('div');
                            mask.classList.add("mui-show-loading-mask");
                            document.body.appendChild(mask);
                            mask.addEventListener("touchmove", function(e){e.stopPropagation();e.preventDefault();});
                        }else{
                            mask[0].classList.remove("mui-show-loading-mask-hidden");
                        }
                        //加载框
                        var toast=document.getElementsByClassName("mui-show-loading");
                        if(toast.length==0){
                            toast = document.createElement('div');
                            toast.classList.add("mui-show-loading");
                            toast.classList.add('loading-visible');
                            document.body.appendChild(toast);
                            toast.innerHTML = html;
                            toast.addEventListener("touchmove", function(e){e.stopPropagation();e.preventDefault();});
                        }else{
                            toast[0].innerHTML = html;
                            toast[0].classList.add("loading-visible");
                        }
                    }
                };

                //隐藏加载框
                $.hideLoading = function(callback) {
                    if ($.os.plus) {
                        $.plusReady(function() {
                            plus.nativeUI.closeWaiting();
                        });
                    }
                    var mask=document.getElementsByClassName("mui-show-loading-mask");
                    var toast=document.getElementsByClassName("mui-show-loading");
                    if(mask.length>0){
                        mask[0].classList.add("mui-show-loading-mask-hidden");
                    }
                    if(toast.length>0){
                        toast[0].classList.remove("loading-visible");
                        callback && callback();
                    }
                }
            })(mui, window);
            if(window.localStorage.data_account!='' && window.localStorage.data_account!=undefined){
                mui.showLoading("正在加载.."); //加载文字和类型，plus环境中类型为div时强制以div方式显示
                //已存在，在判断是否已经被关闭
                $.ajax({
                    type: "post",
                    url: "/wap/serviced/autologin",
                    data: { account: window.localStorage.data_account, password: window.localStorage.data_pwd,deviceid:Config.deviceid},
                    success:function(res){
                        console.log(res);
                        mui.hideLoading();//隐藏后的回调函数
                        if(res.code==0){
                            mui.alert(res.msg);
                            mui.showLoading("不可操作.."); //加载文字和类型，plus环境中类型为div时强制以div方式显示
                        }
                        return false;
                    }
                });
            }

            $('input[type="radio"][name="row[gufengji]"]').click(function() {
                // console.log('Radio button clicked!');
                // console.log('Selected value: ' + $(this).val());
                if($(this).val()=='不正常'){
                    $(".gufengji_other").css('display','block');
                    $('form[role=form]').validator("setField", {
                        "row[gufengji_pic]": "required",
                    });
                }else{
                    $(".gufengji_other").css('display','none');
                    $('form[role=form]').validator("setField", {
                        "row[gufengji_pic]": null,
                    });
                }
            });
            $('input[type="radio"][name="row[tishengbeng]"]').click(function() {
                if($(this).val()=='不正常'){
                    $(".tishengbeng_other").css('display','block');
                    $('form[role=form]').validator("setField", {
                        "row[tishengbeng_pic]": "required",
                    });
                }else{
                    $(".tishengbeng_other").css('display','none');
                    $('form[role=form]').validator("setField", {
                        "row[tishengbeng_pic]": null,
                    });
                }
            });
            $('input[type="radio"][name="row[huiliubeng]"]').click(function() {
                if($(this).val()=='不正常'){
                    $(".huiliubeng_other").css('display','block');
                    $('form[role=form]').validator("setField", {
                        "row[huiliubeng_pic]": "required",
                    });
                }else{
                    $(".huiliubeng_other").css('display','none');
                    $('form[role=form]').validator("setField", {
                        "row[huiliubeng_pic]": null,
                    });
                }
            });
            $('input[type="radio"][name="row[tishengguan]"]').click(function() {
                // console.log('Radio button clicked!');
                // console.log('Selected value: ' + $(this).val());
                if($(this).val()=='不正常'){
                    $(".tishengguan_other").css('display','block');
                    $('form[role=form]').validator("setField", {
                        "row[tishengguan_pic]": "required",
                    });
                }else{
                    $(".tishengguan_other").css('display','none');
                    $('form[role=form]').validator("setField", {
                        "row[tishengguan_pic]": null,
                    });
                }
            });
            $('input[type="radio"][name="row[huiliuguan]"]').click(function() {
                // console.log('Radio button clicked!');
                // console.log('Selected value: ' + $(this).val());
                if($(this).val()=='不正常'){
                    $(".huiliuguan_other").css('display','block');
                    $('form[role=form]').validator("setField", {
                        "row[huiliuguan_pic]": "required",
                    });
                }else{
                    $(".huiliuguan_other").css('display','none');
                    $('form[role=form]').validator("setField", {
                        "row[huiliuguan_pic]": null,
                    });
                }
            });
            $('input[type="radio"][name="row[jiaobanqi]"]').click(function() {
                // console.log('Radio button clicked!');
                // console.log('Selected value: ' + $(this).val());
                if($(this).val()=='不正常'){
                    $(".jiaobanqi_other").css('display','block');
                    $('form[role=form]').validator("setField", {
                        "row[jiaobanqi_pic]": "required",
                    });
                }else{
                    $(".jiaobanqi_other").css('display','none');
                    $('form[role=form]').validator("setField", {
                        "row[jiaobanqi_pic]": null,
                    });
                }
            });

            //2
            $('input[type="radio"][name="row[luocigufengji]"]').click(function() {
                if($(this).val()!='正常'){
                    $(".luocigufengji_other").css('display','block');
                    $('form[role=form]').validator("setField", {
                        "row[luocigufengji_pic]": "required",
                    });
                }else{
                    $(".luocigufengji_other").css('display','none');
                    $('form[role=form]').validator("setField", {
                        "row[luocigufengji_pic]": null,
                    });
                }
            });
            $('input[type="radio"][name="row[huixuangufengji]"]').click(function() {
                if($(this).val()!='正常'){
                    $(".huixuangufengji_other").css('display','block');
                    $('form[role=form]').validator("setField", {
                        "row[huixuangufengji_pic]": "required",
                    });
                }else{
                    $(".huixuangufengji_other").css('display','none');
                    $('form[role=form]').validator("setField", {
                        "row[huixuangufengji_pic]": null,
                    });
                }
            });
            $('input[type="radio"][name="row[kongqibengfengji]"]').click(function() {
                if($(this).val()!='正常'){
                    $(".kongqibengfengji_other").css('display','block');
                    $('form[role=form]').validator("setField", {
                        "row[kongqibengfengji_pic]": "required",
                    });
                }else{
                    $(".kongqibengfengji_other").css('display','none');
                    $('form[role=form]').validator("setField", {
                        "row[kongqibengfengji_pic]": null,
                    });
                }
            });

            // 3场地
            $('input[type="radio"][name="row[changdi]"]').click(function() {
                if($(this).val()=='不正常'){
                    $(".changdi_other").css('display','block');
                    $(".sbchangdi_pic").css('display','none');
                    $('form[role=form]').validator("setField", {
                        "row[changdi_pic]": null,
                        "row[changdi_pic1]": 'required',
                        "row[changdi_pic2]": 'required',
                    });
                }else{
                    $(".changdi_other").css('display','none');
                    $(".sbchangdi_pic").css('display','block');
                    $('form[role=form]').validator("setField", {
                        "row[changdi_pic]": 'required',
                        "row[changdi_pic1]": null,
                        "row[changdi_pic2]": null,
                    });
                }
            });
            //设备间
            $('input[type="radio"][name="row[shebeijian]"]').click(function() {
                if($(this).val()=='不正常'){
                    $(".shebeijian_other").css('display','block');
                    $(".shebeijian_pic").css('display','none');
                    $('form[role=form]').validator("setField", {
                        "row[shebeijian_pic]": null,
                        "row[shebeijian_pic1]": 'required',
                        "row[shebeijian_pic2]": 'required',
                    });
                }else{
                    $(".shebeijian_other").css('display','none');
                    $(".shebeijian_pic").css('display','block');
                    $('form[role=form]').validator("setField", {
                        "row[shebeijian_pic]": 'required',
                        "row[shebeijian_pic1]": null,
                        "row[shebeijian_pic2]": null,
                    });
                }
            });


            $('input[type="radio"][name="row[chushuikou]"]').click(function() {
                if($(this).val()=='拍照'){
                    $(".chushuikou_other").css('display','block');
                    $('form[role=form]').validator("setField", {
                        "row[chushuikou_pic]": "required",
                    });
                }else{
                    $(".chushuikou_other").css('display','none');
                    $('form[role=form]').validator("setField", {
                        "row[chushuikou_pic]": null,
                    });
                }
            });
            $('input[type="radio"][name="row[boli]"]').click(function() {
                // console.log('Radio button clicked!');
                // console.log('Selected value: ' + $(this).val());
                if($(this).val()=='不正常'){
                    $(".boli_other").css('display','block');
                    $('form[role=form]').validator("setField", {
                        "row[boli_pic]": "required",
                    });
                }else{
                    $(".boli_other").css('display','none');
                    $('form[role=form]').validator("setField", {
                        "row[boli_pic]": null,
                    });
                }
            });

            // 4
            $('input[type="radio"][name="row[xiwu]"]').click(function() {
                // console.log('Radio button clicked!');
                // console.log('Selected value: ' + $(this).val());
                if($(this).val()!='正常'){
                    $(".xiwu_other").css('display','block');
                    $('form[role=form]').validator("setField", {
                        "row[xiwu_pic]": "required",
                    });
                }else{
                    $(".xiwu_other").css('display','none');
                    $('form[role=form]').validator("setField", {
                        "row[xiwu_pic]": null,
                    });
                }
            });
            setInterval(function() {
                if($(".mui-tips").css("display") !== "none"){
                    $(".mui-tips").fadeOut();
                }
            }, 3000); // 每3000毫秒（3秒）执行一次
            Controller.api.upload();
            Form.api.bindevent($("#login-form"));
        },
        supple: function () {
            Controller.api.upload();
            Form.api.bindevent($("#login-form"));
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            upload:function(){
                var allowTypes = ['image/jpg', 'image/jpeg', 'image/png', 'image/gif'];
                // 1024KB，也就是 1MB
                var maxSize = 1024 * 1024;
                // 图片最大宽度
                var maxWidth = 800;
                var maxHeight = 800;
                var pic_config = {
                    width: 700,
                    height: 700,
                    quality: 1
                };
                // 最大上传图片数量
                var maxCount = 1;

                var tmpl = '<li class="weui-uploader__file weui-uploader__file_status" id="#ImgID#" style="background-image:url(#url#)"> <div class="weui-uploader__file-content"></div></li>',
                    $gallery = $("#gallery"),
                    $galleryImg = $("#galleryImg"),
                    $uploaderInput101 = $(".uploaderInput101"),
                    $galleryDelete = $(".weui-gallery__opr"),
                    $previewer = $(".previewer");

                    $uploaderInput101.on("change", function (event) {
                    var src, url = window.URL || window.webkitURL || window.mozURL, files = event.target.files;
                    if (files.length === 0) {
                        return;
                    }

                    var fd = new FormData();
                    for (var i = 0, len = files.length; i < len; i++) {
                        var file = files[i];
                        var fileType = file.type;
                        var reader = new FileReader();
                        //     // 如果类型不在允许的类型范围内
                        if (allowTypes.indexOf(file.type) === -1) {
                            $.toast('该类型不允许上传' + fileType, "forbidden");
                            continue;
                        }
                        if ($('#uploaderFiles > .weui-uploader__file').length >= maxCount) {
                            $.toast('最多只能上传' + maxCount + '张图片', "forbidden");
                            return;
                        }
                        src = url.createObjectURL(file);
                        var img = new Image();
                        var types = $(this).attr('types');
                        (function (i) {
                            Controller.api.compressImage(src, pic_config, function (result) {
                                fd.append("file", result);
                                $.ajax({
                                    url: "/wap/serviced/upload",
                                    type: "POST",
                                    data: fd,
                                    dataType: 'json',
                                    processData: false,  // 告诉jQuery不要去处理发送的数据
                                    contentType: false,   // 告诉jQuery不要去设置Content-Type请求头
                                    async: false,
                                    success: function (res) {
                                        console.log(res.url);
                                        if(types=='person'){
                                            $(".persons_images").val(res.url);
                                            $(".persons_pic").attr('src',res.url);
                                            $(".persons_pic").css('display','block');
                                        }else if(types=="gufengji"){//鼓风机
                                            $(".gufengji_images").val(res.url);
                                            $(".gufengji_pic").attr('src',res.url);
                                            $(".gufengji_pic").css('display','block');
                                        }else if(types=="tishengbeng"){//提升泵
                                            $(".tishengbeng_images").val(res.url);
                                            $(".tishengbeng_pic").attr('src',res.url);
                                            $(".tishengbeng_pic").css('display','block');
                                        }else if(types=="tishengbeng1"){//提升泵 特种工作人员手持照片
                                            $(".tishengbeng_images1").val(res.url);
                                            $(".tishengbeng_pic1").attr('src',res.url);
                                            $(".tishengbeng_pic1").css('display','block');
                                        }else if(types=="huiliubeng"){//回流泵
                                            $(".huiliubeng_images").val(res.url);
                                            $(".huiliubeng_pic").attr('src',res.url);
                                            $(".huiliubeng_pic").css('display','block');
                                        }else if(types=="huiliubeng1"){//回流泵 特种工作人员手持照片
                                            $(".huiliubeng_images1").val(res.url);
                                            $(".huiliubeng_pic1").attr('src',res.url);
                                            $(".huiliubeng_pic1").css('display','block');
                                        }else if(types=="tishengguan"){ //提升管
                                            $(".tishengguan_images").val(res.url);
                                            $(".tishengguan_pic").attr('src',res.url);
                                            $(".tishengguan_pic").css('display','block');
                                        }else if(types=="huiliuguan"){//回流管
                                            $(".huiliuguan_images").val(res.url);
                                            $(".huiliuguan_pic").attr('src',res.url);
                                            $(".huiliuguan_pic").css('display','block');
                                        }else if(types=="jiaobanqi"){//搅拌器
                                            $(".jiaobanqi_images").val(res.url);
                                            $(".jiaobanqi_pic").attr('src',res.url);
                                            $(".jiaobanqi_pic").css('display','block');
                                        }else if(types=="jiaobanqi1"){//搅拌器 特种工作人员手持照片
                                            $(".jiaobanqi_images1").val(res.url);
                                            $(".jiaobanqi_pic1").attr('src',res.url);
                                            $(".jiaobanqi_pic1").css('display','block');
                                        }else if(types=="luocigufengji"){//罗茨鼓风机
                                            $(".luocigufengji_images").val(res.url);
                                            $(".luocigufengji_pic").attr('src',res.url);
                                            $(".luocigufengji_pic").css('display','block');
                                        }else if(types=="huixuangufengji"){//回转鼓风机
                                            $(".huixuangufengji_images").val(res.url);
                                            $(".huixuangufengji_pic").attr('src',res.url);
                                            $(".huixuangufengji_pic").css('display','block');
                                        }else if(types=="kongqibengfengji"){//空气泵风机
                                            $(".kongqibengfengji_images").val(res.url);
                                            $(".kongqibengfengji_pic").attr('src',res.url);
                                            $(".kongqibengfengji_pic").css('display','block');
                                        }else if(types=='changdi'){//场地整理情况 片
                                            $(".changdi_images").val(res.url);
                                            $(".changdi_pic").attr('src',res.url);
                                            $(".changdi_pic").css('display','block');
                                        }else if(types=='changdi1'){ //场地整理情况 整改前照片
                                            $(".changdi_images1").val(res.url);
                                            $(".changdi_pic1").attr('src',res.url);
                                            $(".changdi_pic1").css('display','block');
                                        }else if(types=='changdi2'){ //场地整理情况 整改前照片
                                            $(".changdi_images2").val(res.url);
                                            $(".changdi_pic2").attr('src',res.url);
                                            $(".changdi_pic2").css('display','block');
                                        }else if(types=='shebeijian'){ //设备间整体情况
                                            $(".shebeijian_images").val(res.url);
                                            $(".shebeijian_pic").attr('src',res.url);
                                            $(".shebeijian_pic").css('display','block');
                                        }else if(types=='shebeijian1'){//设备间不正常 整改前
                                            $(".shebeijian_images1").val(res.url);
                                            $(".shebeijian_pic1").attr('src',res.url);
                                            $(".shebeijian_pic1").css('display','block');
                                        }else if(types=='shebeijian2'){//设备间不正常 整改前
                                            $(".shebeijian_images2").val(res.url);
                                            $(".shebeijian_pic2").attr('src',res.url);
                                            $(".shebeijian_pic2").css('display','block');
                                        }else if(types=='chushuikou'){//出水口卫生情况
                                            $(".chushuikou_images").val(res.url);
                                            $(".chushuikou_pic").attr('src',res.url);
                                            $(".chushuikou_pic").css('display','block');
                                        }else if(types=='boli'){//设施玻璃钢罐体 
                                            $(".boli_images").val(res.url);
                                            $(".boli_pic").attr('src',res.url);
                                            $(".boli_pic").css('display','block');
                                        }else if(types=='xiwu'){ //吸污加菌情况
                                            $(".xiwu_images").val(res.url);
                                            $(".xiwu_pic").attr('src',res.url);
                                            $(".xiwu_pic").css('display','block');
                                        }
                                    }
                                });
                            })
                        })(i)
                    }
                });

                
            },
            convertBase64UrlToBlob: function (urlData) {
                console.log(urlData);
                var arr = urlData.split(',')
                var mime = arr[0].match(/:(.*?);/)[1]
                console.log(mime)
                var bstr = atob(arr[1])
                var n = bstr.length
                var u8arr = new Uint8Array(n)
                while (n--) {
                    u8arr[n] = bstr.charCodeAt(n)
                }
                return new Blob([u8arr], {type: mime})
            },
            convertBase64UrltoFile: function (dataurl) {
                var filename = 'file'
                var arr = dataurl.split(',')
                var mime = arr[0].match(/:(.*?);/)[1]
                var suffix = mime.split('/')[1]
                var bstr = atob(arr[1])
                var n = bstr.length
                var u8arr = new Uint8Array(n)
                while (n--) {
                    u8arr[n] = bstr.charCodeAt(n)
                }
                return new File([u8arr], filename + '.' + suffix, {
                    type: mime
                })
            },
            compressImage: function (path, config, callback) {

                var img = new Image()
                img.src = path
                img.onload = function () {
                    var that = this
                    var width_ratio = config.width / that.width;
                    var height_ratio = config.height / that.height;
                    if (width_ratio >= height_ratio) {
                        var w = Math.min(config.width, that.width);
                        // 高度按比例计算
                        var h = that.height * (w / that.width);
                    }
                    // 源图过宽
                    else if (width_ratio <= height_ratio) {

                        var h = Math.min(config.height, that.height);
                        var w = that.width * (h / that.height);
                    }
                    var quality = 0.7 // 默认图片质量为0.7
                    // 生成canvas
                    var canvas = document.createElement('canvas')
                    var ctx = canvas.getContext('2d')
                    // 创建属性节点
                    var anw = document.createAttribute('width')
                    anw.nodeValue = w
                    var anh = document.createAttribute('height')
                    anh.nodeValue = h
                    canvas.setAttributeNode(anw)
                    canvas.setAttributeNode(anh)
                    ctx.drawImage(that, 0, 0, w, h)
                    if (config.quality && config.quality <= 1 && config.quality > 0) {
                        quality = config.quality
                    }
                    var base64 = canvas.toDataURL('image/*', quality)
                    var blob = Controller.api.convertBase64UrltoFile(base64)
                    // 回调函数返回blob的值（若不需要，可直接返回base64的值）
                    callback(blob)
                }
            },
        }
    }
    return Controller;
})
